from django.db import models
from django.forms import widgets

from XApiTestServer.managers import UserTypeManager, UserInfoManager, ProjectInfoManager, ModuleInfoManager, \
    TestCaseInfoManager, TestConfigInfoManager, FileInfoManager
import django.utils.timezone as timezone


# Create your models here.


class BaseTable(models.Model):
    """
    公共字段列
    """
    class Meta:
        abstract = True
        verbose_name = "公共字段表"
        db_table = 'BaseTable'

    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)


class UserType(BaseTable):
    class Meta:
        verbose_name = '用户类型'
        db_table = 'User_Type'

    type_name = models.CharField(max_length=20)
    type_desc = models.CharField(max_length=50)
    objects = UserTypeManager()


class UserInfo(BaseTable):
    class Meta:
        verbose_name = '用户信息'
        db_table = 'User_Info'

    username = models.CharField('用户名', max_length=20, unique=True)
    password = models.CharField('密码', max_length=20)
    email = models.EmailField('邮箱', null=False, unique=True)
    status = models.IntegerField('有效/无效', default=1)
    # user_type = models.ForeignKey(UserType, on_delete=models.CASCADE)

    objects = UserInfoManager()


class UserToken(BaseTable):
    """
    用户登陆token
    """
    class Meta:
        verbose_name = "用户登陆token"
        db_table = "User_Token"

    user = models.OneToOneField(to=UserInfo, on_delete=models.CASCADE)
    token = models.CharField('token', max_length=50)


class ProjectInfo(BaseTable):
    class Meta:
        verbose_name = '项目信息'
        db_table = 'Project_Info'

    project_name = models.CharField('项目名称', max_length=50, unique=True)
    responsible_name = models.CharField('负责人', max_length=20)
    test_user = models.CharField('测试人员', max_length=100)
    dev_user = models.CharField('开发人员', max_length=100)
    publish_app = models.CharField('发布应用', max_length=100)
    simple_desc = models.CharField('简要描述', max_length=100, null=True)
    other_desc = models.CharField('其他信息', max_length=100, null=True)

    objects = ProjectInfoManager()


class ModuleInfo(BaseTable):
    class Meta:
        verbose_name = '模块信息'
        db_table = 'Module_Info'

    module_name = models.CharField('模块名称', max_length=50)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    test_user = models.CharField('测试负责人', max_length=50)
    simple_desc = models.CharField('简要描述', max_length=100, null=True)
    other_desc = models.CharField('其他信息', max_length=100, null=True)

    objects = ModuleInfoManager()


class TestAuth(BaseTable):
    class Meta:
        verbose_name = '用例鉴权信息管理'
        db_table = 'Test_Auth'

    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    name = models.CharField('认证名称', max_length=100)
    auth_type = models.CharField('鉴权方式', max_length=50)
    auth_url = models.CharField("获取鉴权地址", max_length=100,  default='@@@auth_url@@@')
    request = models.TextField('请求信息', null=True)
    user_name = models.CharField('用户名称', max_length=50, null=True)
    password = models.CharField('密码', max_length=50, default='', null=True)
    author = models.CharField('添加人员', max_length=50, null=True)


class TestCase(BaseTable):
    class Meta:
        verbose_name = '用例信息'
        db_table = 'Test_Case'

    name = models.CharField('测试用例名称', max_length=100)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    belong_module = models.ForeignKey(ModuleInfo, on_delete=models.CASCADE)
    pre_cases = models.CharField('前置测试用例', max_length=1024, null=True)
    request = models.TextField('请求信息')
    auth = models.ForeignKey(TestAuth, null=True, on_delete=models.SET_NULL)
    author = models.CharField('编写人员', max_length=20)


class TestConfig(BaseTable):
    class Meta:
        verbose_name = '测试环境和配置信息'
        db_table = 'Test_Config'

    name = models.CharField('配置名称', max_length=100)
    # belong_project = models.CharField('所属项目', max_length=50)
    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    config_body = models.TextField('配置内容')
    author = models.CharField('编写人员', max_length=20)


class TestReports(BaseTable):
    class Meta:
        verbose_name = "测试报告"
        db_table = 'Test_Report'

    report_name = models.CharField("报告名称", max_length=100)
    start_at = models.CharField("允许开始时间", max_length=40, null=True)
    status = models.BooleanField()
    testsRun = models.IntegerField()
    successes = models.IntegerField()
    reports = models.TextField()
    author = models.CharField('运行人员', max_length=100, default='')


class TestSuite(BaseTable):
    class Meta:
        verbose_name = '用例集合'
        db_table = 'Test_Suite'

    belong_project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE)
    suite_name = models.CharField(max_length=100)
    include = models.TextField()
    disabled_cnt = models.IntegerField('停用的用例数目', default=0)
    author = models.CharField('编写人员', max_length=20)


class FileInfo(BaseTable):
    class Meta:
        verbose_name = '上传文件管理'
        db_table = 'File_Info'

    name = models.CharField('文件名称', max_length=200)
    belong_project = models.CharField('所属项目', max_length=50)
    belong_module = models.CharField('所属模块', max_length=50)

    file_path = models.CharField('文件路径', max_length=250, default='')
    create_time = models.DateTimeField('上传时间', default=timezone.now)
    file_size = models.IntegerField('文件大小', default=0)
    project = models.ForeignKey(ProjectInfo, on_delete=models.CASCADE, default=1)
    module = models.ForeignKey(ModuleInfo, on_delete=models.CASCADE, default=1)

    author = models.CharField('上传人员', max_length=20)

    objects = FileInfoManager()


class MockInfo(BaseTable):
    class Meta:
        verbose_name = 'Mock信息'
        db_table = 'Mock_Info'

    name = models.CharField('Mock名称', max_length=100)
    mock_url = models.CharField('Mock URL', max_length=100)
    request_method = models.CharField('Mock请求方式', max_length=10)
    request = models.TextField('请求信息', null=True)
    response_body = models.TextField('Mock响应信息body')
    author = models.CharField('编写人员', max_length=20)
